package net.googlecode.vasetskiy.task5;

import net.googlecode.vasetskiy.task5.calculator.Calculator;
import net.googlecode.vasetskiy.task5.calculator.SalaryCalculatorFactory;
import net.googlecode.vasetskiy.task5.model.Boss;
import net.googlecode.vasetskiy.task5.model.Employee;
import net.googlecode.vasetskiy.task5.model.Manager;
import net.googlecode.vasetskiy.task5.calculator.Sorted;

import java.math.BigDecimal;
import java.util.*;


/**
 * Main application for calculating salary for person
 */
public class SalaryApp {
    private static final BigDecimal BOSS_TAX = new BigDecimal(500);
    private static final BigDecimal MANAGER_TAX = new BigDecimal(250);
    private static final BigDecimal EMPLOYEE_TAX = new BigDecimal(125);

    public static void main(String[] args) {
            Sorted out = new Sorted();
            Manager boss = createBoss();
            Manager manager1 = createManager();
            Manager manager2 = createManagerTwo();
            Manager manager3 = createManagerThree();
            Manager employee1 = createEmployee();
            Manager employee2 = createEmployeeTwo();
            if (args.length > 0) {
                if (args[0].equals("code")) {
                    out.sorted(createCollection(boss, manager1, employee1, manager2, manager3, employee2));
                } else {
                    if (args[0].equals("name")) {
                        out.lastNameSorted(createCollection(boss, manager1, employee1, manager2, manager3, employee2));
                    } else {
                        if (args[0].equals("salary")) {
                            out.salarySorted(createCollection(boss, manager1, employee1, manager2, manager3, employee2));
                        } else {
                            System.out.println("Incorrectly entered parameter");

                        }
                    }
                }
            }
        }

        private static Collection<Manager> createCollection(Manager... employees) {
            Collection<Manager> stuff = new ArrayList<Manager>();

            for (Manager x : employees) {
                boolean unic = true;
                Iterator<Manager> it = stuff.iterator();
                while (it.hasNext()) {
                    if (it.next().getPersonID() == x.getPersonID()) {
                        unic = false;
                    }
                }
                if (unic) {
                    stuff.add(x);
                } else {
                    //System.out.println("Employee with personal ID = " + x.getPersonalID() + " is already exists.");
                }
            }
            return stuff;
        }


        private static BigDecimal calculateSalary(Manager person, BigDecimal tax) {
            Calculator salaryCalculator = SalaryCalculatorFactory.getCalculator(person);
            salaryCalculator.setTax(tax);
            return salaryCalculator.calculateSalary(person);
        }


    public static Manager createManager() {
        Manager manager = new Manager();
        manager.setPersonID("41335");
        manager.setFirstName("Ivan");
        manager.setLastName("Ivanov");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(false);
        manager.setRate(new BigDecimal(25));
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

    public static Boss createBoss() {
        Boss boss = new Boss();
        boss.setPersonID("11111");
        boss.setFirstName("Petr");
        boss.setLastName("Petrov");
        boss.setAge(35);
        boss.setBonusAmount(new BigDecimal(1000));
        boss.setRate(new BigDecimal(50));
        boss.setSalary(calculateSalary(boss, BOSS_TAX));


        return boss;
    }

    public static Employee createEmployee() {
        Employee employee = new Employee();
        employee.setPersonID("33548");
        employee.setFirstName("Semen");
        employee.setLastName("Semenov");
        employee.setAge(55);
        employee.setBonus(true);
        employee.setBonusAmount(new BigDecimal(1500));
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(8));
        employee.setSalary(new BigDecimal(125));
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

public static Employee createEmployeeTwo() {
        Employee employee = new Employee();
        employee.setPersonID("19513");
        employee.setFirstName("Gennadiy");
        employee.setLastName("Gennadievich");
        employee.setAge(39);
        employee.setBonus(true);
        employee.setBonusAmount(new BigDecimal(2000));
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(9));
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

    public static Manager createManagerTwo() {
        Manager manager = new Manager();
        manager.setPersonID("25572");
        manager.setFirstName("Gektor");
        manager.setLastName("Gektorovich");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(false);
        manager.setRate(new BigDecimal(25));
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

    public static Manager createManagerThree() {
        Manager manager = new Manager();
        manager.setPersonID("67915");
        manager.setFirstName("Kiril");
        manager.setLastName("Kirilovich");
        manager.setAge(45);
        manager.setPerHour(true);
        manager.setBonus(true);
        manager.setBonusAmount(new BigDecimal(1000));
        manager.setRate(new BigDecimal(30));
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }



}
